package com.ruoyi.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import com.ruoyi.system.domain.Myuser;
import com.ruoyi.system.service.IMyuserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.FaceDetection;
import com.ruoyi.system.service.IFaceDetectionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人脸检测管理Controller
 *
 * @author ruoyi
 * @date 2022-09-19
 */
@Controller
@RequestMapping("/system/detection")
public class FaceDetectionController extends BaseController {
    private String prefix = "system/detection";

    @Autowired
    private IFaceDetectionService faceDetectionService;
    @Autowired
    private IMyuserService myuserService;

    @RequiresPermissions("system:detection:view")
    @GetMapping()
    public String detection() {
        return prefix + "/detection";
    }

    /**
     * 查询人脸检测管理列表
     */
//    @RequiresPermissions("system:detection:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FaceDetection faceDetection) {
        startPage();
        List<FaceDetection> list = faceDetectionService.selectFaceDetectionList(faceDetection);
        return getDataTable(list);
    }

    /**
     * 导出人脸检测管理列表
     */
    @RequiresPermissions("system:detection:export")
    @Log(title = "人脸检测管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FaceDetection faceDetection) {
        List<FaceDetection> list = faceDetectionService.selectFaceDetectionList(faceDetection);
        ExcelUtil<FaceDetection> util = new ExcelUtil<FaceDetection>(FaceDetection.class);
        return util.exportExcel(list, "人脸检测管理数据");
    }

    /**
     * 新增人脸检测管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存人脸检测管理
     */
    @RequiresPermissions("system:detection:add")
    @Log(title = "人脸检测管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FaceDetection faceDetection) {
        return toAjax(faceDetectionService.insertFaceDetection(faceDetection));
    }

    /**
     * 修改人脸检测管理
     */
    @RequiresPermissions("system:detection:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        FaceDetection faceDetection = faceDetectionService.selectFaceDetectionById(id);
        mmap.put("faceDetection", faceDetection);
        return prefix + "/edit";
    }

    /**
     * 修改保存人脸检测管理
     */
    @RequiresPermissions("system:detection:edit")
    @Log(title = "人脸检测管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FaceDetection faceDetection) {
        return toAjax(faceDetectionService.updateFaceDetection(faceDetection));
    }

    /**
     * 删除人脸检测管理
     */
    @RequiresPermissions("system:detection:remove")
    @Log(title = "人脸检测管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(faceDetectionService.deleteFaceDetectionByIds(ids));
    }

    /**
     * 查看自己是否被要求人脸检测
     *
     * @return
     */
    @PostMapping("/select")
    @ResponseBody
    public AjaxResult mylook(@RequestParam("phonenunber") String PhoneNumber) {
        FaceDetection faceDetection = new FaceDetection();
        faceDetection.setUsertwoPhone(PhoneNumber);
        faceDetection.setResultStatus("0");
        List<FaceDetection> faceDetections = faceDetectionService.selectFaceDetectionList(faceDetection);
        if (faceDetections.size() != 0) {
            List<Long> ids = new ArrayList<>();
            for (FaceDetection item : faceDetections) {
                ids.add(item.getId());
            }
            return success(ids);
        }
        return AjaxResult.success("不需要检测");
    }

    @PostMapping("/addfacedetection")
    @ResponseBody
    public AjaxResult addfacedetection(@RequestParam("userId") Long userId,
                                       @RequestParam("username") String username,
                                       @RequestParam("userPhone") String userPhone) {

        if (isValidPhoneNumber(userPhone)) {
            Myuser myuser = new Myuser();
            myuser.setPhoneNumber(userPhone);
            List<Myuser> myusers = myuserService.selectMyuserList(myuser);
            if (myusers.size() < 1) {
                return error("该用户未注册本软件");
            }
            FaceDetection faceDetection = new FaceDetection();
            faceDetection.setUserone(userId);
            faceDetection.setUsertwoPhone(userPhone);
            faceDetection.setUsername(username);
            return success(faceDetectionService.insertFaceDetection(faceDetection));
        } else {
            return error("手机号码格式有误");
        }

    }

    public static boolean isValidPhoneNumber(String phoneNumber) {
        if ((phoneNumber != null) && (!phoneNumber.isEmpty())) {
            return Pattern.matches("^1[3-9]\\d{9}$", phoneNumber);
        }
        return false;
    }

    @PostMapping("/setfacedetection")
    @ResponseBody
    public AjaxResult setfacedetection(@RequestParam("id") Long id,
                                       @RequestParam("result") String result) {
        FaceDetection faceDetection = new FaceDetection();
        faceDetection.setId(id);
        faceDetection.setResultStatus(result);
        return success(faceDetectionService.updateFaceDetection(faceDetection));
    }

    @PostMapping("/getuserlist")
    @ResponseBody
    public AjaxResult getresultList(@RequestParam("userid") Long userid){
        FaceDetection faceDetection = new FaceDetection();
        faceDetection.setUserone(userid);
        return success(faceDetectionService.selectFaceDetectionList(faceDetection));
    }
    @PostMapping("/getitemresult")
    @ResponseBody
    public AjaxResult getresult(@RequestParam("id") Long id){
        return success(faceDetectionService.selectFaceDetectionById(id));
    }
}
